package com.caoustc.okhttplib.okhttp.platform

import com.caoustc.okhttplib.okhttp.BaseHttpRequestCallback
import com.caoustc.okhttplib.okhttp.OkHttpMethod
import com.caoustc.okhttplib.okhttp.OkHttpPlatformRequest
import com.caoustc.okhttplib.okhttp.OkHttpRequestParams

@Deprecated(
    """已经弃用，准备删除 3.9.0.1
  modified by caixingming 2020/11/12 .
  Created by cz on 2017/10/16."""
)
class PlatformApiManager(
    val okHttpMethod: OkHttpMethod,
    val url: String?,
    val params: OkHttpRequestParams?,
    private val timeout: Long,
    val parseString: String?,
    val isCancel: Boolean,
    val dataClass: Class<*>?,
    showOnStart: Boolean,
    showOnFailure: Boolean,
    callback: BaseHttpRequestCallback<Any?>?
) {
    var isShowOnStart = false
    var isShowOnFailure = false
    val callback: BaseHttpRequestCallback<Any?>?

    class Builder {
        private var okHttpMethod = OkHttpMethod.POST
        private var parseString: String? = null
        private var isCancel = true
        private var method: String? = null
        private var params: OkHttpRequestParams? = null
        private var timeout: Long = 0
        private var dataClass: Class<*>? = null
        private var showOnStart = false
        private var showOnFailure = false
        private var callback: BaseHttpRequestCallback<Any?>? = null
        fun setOkHttpMethod(method: OkHttpMethod): Builder {
            okHttpMethod = method
            return this
        }

        fun setParseString(parseString: String?): Builder {
            this.parseString = parseString
            return this
        }

        fun setCancel(cancel: Boolean): Builder {
            isCancel = cancel
            return this
        }

        fun setTimeout(timeout: Long): Builder {
            this.timeout = timeout
            return this
        }

        fun setMethod(method: String?): Builder {
            this.method = method
            return this
        }

        fun setParams(params: OkHttpRequestParams?): Builder {
            OkHttpParamsHelper.addBaseOkHttpParams(params, method)
            this.params = OkHttpParamsHelper.sortSign(params)
            return this
        }

        fun setSingleParams(params: OkHttpRequestParams?): Builder {
            this.params = OkHttpParamsHelper.sortSign(params)
            return this
        }

        fun setDataClass(dataClass: Class<*>?): Builder {
            this.dataClass = dataClass
            return this
        }

        fun setShowOnStart(): Builder {
            showOnStart = true
            return this
        }

        fun setShowOnFailure(): Builder {
            showOnFailure = true
            return this
        }

        fun setCallback(callback: BaseHttpRequestCallback<Any?>?): Builder {
            this.callback = callback
            return this
        }

        fun build(): PlatformApiManager {
            return PlatformApiManager(
                okHttpMethod, method, params, timeout, parseString, isCancel,
                dataClass, showOnStart, showOnFailure, callback
            )
        }
    }

    fun start() {
        OkHttpPlatformRequest.execute(
            okHttpMethod, parseString, isCancel, isShowOnStart, isShowOnFailure,
            dataClass, url, params, timeout, callback
        )
    }

    init {
        isShowOnStart = showOnStart
        isShowOnFailure = showOnFailure
        this.callback = callback
    }
}